package com.yzt.zhuju.widget.popmenu;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.content.ContextCompat;


import com.yzt.zhuju.R;
import com.yzt.zhuju.widget.BasePopupWindow;
import com.yzt.p_base.utils.DisplayUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 操作菜单popupwindow
 *
 * @author zhly
 */
public class ActionMenuPopWindow extends BasePopupWindow {

    private final Activity activity;

    private ActionMenuPopWindow(Activity mContext, List<MenuItem> menuItems) {
        super(mContext);
        this.activity = mContext;
        this.mView = buildContentView(menuItems);
        initPopupWindow();
    }

    @Override
    protected void initPopupWindow() {
        super.initPopupWindow();
        popupWindow.setWidth((int) activity.getResources().getDimension(R.dimen.dp_135));
        popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(activity, R.drawable.round_white_10));

        popupWindow.setContentView(mView);
        popupWindow.setElevation(DisplayUtils.dp2px(10));
    }

    private View buildContentView(List<MenuItem> menuItems) {
        LinearLayout contentView = new LinearLayout(activity);
        contentView.setBackgroundResource(R.drawable.round_white_10);
        contentView.setOrientation(LinearLayout.VERTICAL);
        for (int i = 0; i < menuItems.size(); i++) {
            MenuItem menuItem = menuItems.get(i);
            TextView tvItem = new AppCompatTextView(activity);
            tvItem.setText(menuItem.itemTitle);
            tvItem.setTextColor(menuItem.titleColor == 0 ? ContextCompat.getColor(activity, R.color.color_262628) : menuItem.titleColor);
            tvItem.setTextSize(TypedValue.COMPLEX_UNIT_PX, activity.getResources().getDimension(R.dimen.sp_13));
            tvItem.setTypeface(Typeface.DEFAULT_BOLD);
            tvItem.setGravity(Gravity.CENTER);
            LinearLayout.LayoutParams tvLp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) activity.getResources().getDimension(R.dimen.dp_48));
            tvItem.setLayoutParams(tvLp);
            if (menuItem.clickListener != null) {
                tvItem.setOnClickListener(v -> {
                    menuItem.clickListener.onClick();
                    if (menuItem.dismissAfterClick) {
                        dismiss();
                    }
                });
            }
            contentView.addView(tvItem);
            if (i != menuItems.size() - 1) {
                //不是最后一个，加分割线
                View line = new View(activity);
                line.setBackgroundColor(Color.parseColor("#FFF0F0F6"));
                LinearLayout.LayoutParams lineLp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1);
                lineLp.setMarginStart((int) activity.getResources().getDimension(R.dimen.dp_15));
                lineLp.setMarginEnd((int) activity.getResources().getDimension(R.dimen.dp_15));
                line.setLayoutParams(lineLp);
                contentView.addView(line);
            }
        }
        return contentView;
    }

    public static class Builder {
        private Activity activity;
        private final List<MenuItem> menuItems;

        public Builder() {
            menuItems = new ArrayList<>();
        }

        public Builder with(Activity activity) {
            this.activity = activity;
            return this;
        }

        public Builder addItem(MenuItem menuItem) {
            menuItems.add(menuItem);
            return this;
        }

        public ActionMenuPopWindow build() {
            return new ActionMenuPopWindow(activity, menuItems);
        }
    }
}

